<?php

namespace app\admin\controller\member;

use app\common\controller\Backend;
use think\facade\Db;
use Throwable;
/**
 * 成员管理
 */
class Member extends Backend
{
    /**
     * Member模型对象
     * @var object
     * @phpstan-var \app\common\model\Member
     */
    protected object $model;

    protected array|string $preExcludeFields = ['id', 'create_time', 'update_time'];

    protected string|array $quickSearchField = ['id'];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new \app\common\model\Member();
    }


    /**
     * 查看
     * @throws Throwable
     */
    public function index(): void
    {
        if ($this->request->param('select')) {
            $this->select();
        }
        $field = Db::name('HrFieldValue')->where('1=1')->select();
        $field_array = [];
        foreach ($field as $key => $value) {
            $field_array['form_'.$value['field_id']]['value_'.$value['weigh']] = $value['value'];
        }
        list($where, $alias, $limit, $order) = $this->queryBuilder();
        $res = $this->model
            ->field($this->indexField)
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)
            ->where($where)
            ->order($order)
            ->paginate($limit)->each(function($item, $key)use($field_array){
                $item['department_id'] = Db::name('Department')->where('id',$item['department_id'])->value('name');
                $item['form_20006'] = $field_array['form_20006']['value_'.$item['form_20006']]??'';
                $item['form_20010'] = $field_array['form_20010']['value_'.$item['form_20010']]??'';
                $item['form_20011'] = $field_array['form_20011']['value_'.$item['form_20011']]??'';
                $item['form_20012'] = $field_array['form_20012']['value_'.$item['form_20012']]??'';
                $item['form_20013'] = $field_array['form_20013']['value_'.$item['form_20013']]??'';
                $item['form_20014'] = $field_array['form_20014']['value_'.$item['form_20014']]??'';
                $item['form_20015'] = $field_array['form_20015']['value_'.$item['form_20015']]??'';
                $item['form_20016'] = $field_array['form_20016']['value_'.$item['form_20016']]??'';
                $item['form_20017'] = $field_array['form_20017']['value_'.$item['form_20017']]??'';
                $item['form_20018'] = $field_array['form_20018']['value_'.$item['form_20018']]??'';
                $item['form_20019'] = $field_array['form_20019']['value_'.$item['form_20019']]??'';

            });

        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }

    /**
     * 编辑
     * @throws Throwable
     */
    public function edit(): void
    {
        $pk  = $this->model->getPk();
        $id  = $this->request->param($pk);
        $row = $this->model->find($id);
        if (!$row) {
            $this->error(__('Record not found'));
        }

        $dataLimitAdminIds = $this->getDataLimitAdminIds();
        if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
            $this->error(__('You have no permission'));
        }

        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (!$data) {
                $this->error(__('Parameter %s can not be empty', ['']));
            }

            $data   = $this->excludeFields($data);
            $result = false;
            $this->model->startTrans();
            try {
                // 模型验证
                if ($this->modelValidate) {
                    $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                    if (class_exists($validate)) {
                        $validate = new $validate();
                        if ($this->modelSceneValidate) $validate->scene('edit');
                        $data[$pk] = $row[$pk];
                        $validate->check($data);
                    }
                }
                $result = $row->save($data);
                $this->model->commit();
            } catch (Throwable $e) {
                $this->model->rollback();
                $this->error($e->getMessage());
            }
            if ($result !== false) {
                $this->success(__('Update successful'));
            } else {
                $this->error(__('No rows updated'));
            }
        }
        $info = $row->toArray();
        foreach ($info as $key => &$value) {
            if(empty($value)){
                $value = null;
            }
        }
        
        $this->success('', [
            'row' => $info
        ]);
    }

}